package com.hopital5.zhangyz.service;

import com.hopital5.zhangyz.model.ActivityActivityItem;
import com.hopital5.zhangyz.repository.ActivityActivityItemRepository;
import com.hopital5.zhangyz.utils.ServiceException;
import com.hopital5.zhangyz.vo.request.ActivityActivityItemVoRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ActivityActivityItemService {
    @Autowired
    ActivityActivityItemRepository activityActivityItemRepository;

    public ActivityActivityItem save(ActivityActivityItemVoRequest activityActivityItemVoRequest) {
        return null;
    }

    @Transactional
    public List<ActivityActivityItem> update(List<ActivityActivityItemVoRequest> activityActivityItemVoRequests) {
        return activityActivityItemVoRequests.stream().map(activityActivityItemVoRequest -> {
            ActivityActivityItem activityActivityItem = activityActivityItemRepository.findByActivity_IdAndActivityItem_IdAndPeople_Id(activityActivityItemVoRequest.getActivityId(),
                    activityActivityItemVoRequest.getActivityItemId(), activityActivityItemVoRequest.getPeopleId());
            if (activityActivityItem.getNumber().compareTo(activityActivityItemVoRequest.getNumberOld()) != 0) {
                throw new ServiceException("修改失败，其他用户已经修改");
            }
            activityActivityItem.setNumber(activityActivityItemVoRequest.getNumber());
            return activityActivityItem;
        }).collect(Collectors.toList());
    }

    public ActivityActivityItem findOne(Long id) {
        return activityActivityItemRepository.findOne(id);
    }

    public void delete(Long id) {
        activityActivityItemRepository.delete(id);
    }

    public void delete(ActivityActivityItem activityActivityItem) {
        activityActivityItemRepository.delete(activityActivityItem);
    }
}

